//
//  LCTabBar.m
//  OCWeibo
//
//  Created by admin.
//  Copyright © 2017年 __MyCompanyName__. All rights reserved.
//

#import "LCTabBar.h"
#import "TheHomeManager+Additions.h"

@interface LCTabBar () {}

@property (nonatomic,weak) IBOutlet UIButton *plusBtn;

@end

@implementation LCTabBar

-(UIButton *)plusBtn{
        //设置plusBtn 中间按钮
//    CGFloat w = self.bounds.size.width / 5;
//    CGFloat h =  self.bounds.size.height;
    
    UIButton *btn = [[UIButton alloc] init];
    _plusBtn = btn;
//    btn.center = CGPointMake(100, 20);
    btn.bounds = CGRectMake(0, 0, 40, 40);
//    NSLog(@"btn.center = %f %f", btn.center.x,btn.center.y);
//    NSLog(@"btn.center = %f %f", w,h);

    //设置图片 和 背景图片
    
        [_plusBtn setImage:[UIImage imageNamed:@"tab_play"] forState:UIControlStateNormal];
        [_plusBtn setImage:[UIImage imageNamed:@"tab_play"] forState:UIControlStateSelected];
    
    [self addSubview:btn];

//    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.offset(0);
//        make.centerY.equalTo(self.mas_centerY).offset(- self.bounds.size.height * 0.5);
//        make.width.offset([UIScreen mainScreen].bounds.size.width / 5);
//        make.height.equalTo(self.mas_height);
//    }];
    
    return btn;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(musicPlayerStatusDidChangedNotification:) name:LCMusicPlayerStatusDidChangedNotification object:_LCPlayer];
        [self setupUI];
    }
    return self;
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)setupUI{
    
    UIImage *i = [UIImage coloreImage:[UIColor whiteColor]];
    [self setBackgroundImage:i];
    [self setShadowImage:i];

    [self.plusBtn addTarget:self action:@selector(btnClcik) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)musicPlayerStatusDidChangedNotification:(NSNotification *)notification {
    // 获取当前播放状态
    LCMusicPlayerStatus kstatus = [notification.userInfo[kMusicPlayerStatus] integerValue];
    [self refreshUIWithStatus:kstatus];
}
// 播放状态和界面之间的关系
- (void)refreshUIWithStatus:(LCMusicPlayerStatus)status {
    // 告诉播放按钮状态和播放器内部的状态之间的关系
    _plusBtn.selected = status == LCMusicPlayerStatusPlaying;
    if (_plusBtn.selected) {
        [_plusBtn setImage:[UIImage imageNamed:@"tab_pause"] forState:UIControlStateNormal];
        [_plusBtn setImage:[UIImage imageNamed:@"tab_pause"] forState:UIControlStateSelected];
    }else{
        [_plusBtn setImage:[UIImage imageNamed:@"tab_play"] forState:UIControlStateNormal];
        [_plusBtn setImage:[UIImage imageNamed:@"tab_play"] forState:UIControlStateSelected];
    }
}

-(void)btnClcik{
    //点击方
    if (_btnClcikBlock != nil) {
        _btnClcikBlock();
    }
    if (_LCPlayer.kstatus == LCMusicPlayerStatusPlaying) {
        return;
//        [_LCPlayer pause];
    } else {
        //在清空播放列表时也进行暂停. 当没有播放时就提示.
        if (_LCPlayer.songs.count == 0) {
            NSString *titleStr = [LCHistoryDataManager loadBibleTextTitleData];
            if (titleStr) {
                
            }else{
                titleStr = @"旧约创世记(创)第1章";
            }
            [[TheHomeManager shareTheHomeManager] localShengJingSongMessage:[_TheHomeM getSongParentIdWithTitle:titleStr]];
            LCPlaylist *playlistTest = _PlaylistMgr.allPlaylists.firstObject;
            _LCPlayer.songs = playlistTest.allSongs;
            
            /**没有播放圣经*/
            if (_LCPlayer.songs != nil) {
                [_LCPlayer playAtIndex:[[titleStr substringWithinBoundsLeft:@"第" right:@"章"] integerValue] -1 isSelectPlay:YES];
            }
            return;
        }
        [_LCPlayer play];
    }
}


//修改一个控件上同一个类型的控件的frame
///需要在layoutSubviews方法中完成frame的修改
-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat w = self.bounds.size.width / 5;
    CGFloat h =  self.bounds.size.height;
    if (iPhoneX) {
        h =  self.bounds.size.height - 25;
    }
    
    
    CGFloat index = 0;
    Class clz = NSClassFromString(@"UITabBarButton");
    for (UIView *subView in [self subviews]) {

        if ([subView isKindOfClass:clz]) {
//            if (index == 2) {
//                subView.frame = CGRectMake(w * index, 0, w, 2*h);
//                return;
//            }
            subView.frame = CGRectMake(w * index, 0, w, h);
            index += (index == 1 ? 2 : 1);
        }

    }
    _plusBtn.center = CGPointMake(self.center.x, self.bounds.size.height * 0.5);
        _plusBtn.bounds = CGRectMake(0, 0, w, 2*h);
    if (iPhoneX) {
        _plusBtn.center = CGPointMake(self.center.x, self.bounds.size.height * 0.39);
    }
    [self addSubview:_plusBtn];


}
@end
